﻿{include file="public/header"/}
</head>
<body>
<div class="layui-fluid" id="LAY-component-layer-list">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">账号：</label>
                        <div class="layui-input-inline mr0">
                            <input name="user_name" class="layui-input" type="text" placeholder="输入账号"/>
                        </div>
                    </div>                    
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">手机：</label>
                        <div class="layui-input-inline mr0">
                            <input name="mobile" class="layui-input" type="text" placeholder="输入手机"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchUser" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button class="layui-btn icon-btn" data-type="admin_add">
                        <i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>
            <table class="layui-table" id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>
<!-- 表格状态列 -->
<script type="text/html" id="tableStateUser">
<input type="checkbox" lay-filter="ckStateUser" data-id="{{d.admin_id}}" value="{{d.status}}" lay-skin="switch" lay-text="正常|锁定" {{d.status==1 ? 'checked':''}}/>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="tableBarUser">
<a class="layui-btn layui-btn-primary layui-btn-sm" data-type="admin_edit" data-id="{{d.admin_id}}">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-sm" data-type="admin_del" data-url="{:url('/admin/del')}" data-id="{{d.admin_id}}">删除</a>
<a class="layui-btn layui-btn-sm" data-type="admin_reset" data-name="{{d.user_name}}" data-id="{{d.admin_id}}">重置密码</a>
</script>
{include file="public/footer"/}
<script>
    layui.use(['layer', 'form', 'table', 'util'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        
        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: "{:url('/admin/index')}",
            page: true,
            cellMinWidth: 100,
            cols: [[
                {field:'admin_id',title:'ID',width:60},
                {field: 'user_name', title: '账号'},
                {field: 'pen_name', title: '昵称'},
                {field: 'mobile', title: '手机'},
                {field: 'email',  title: '邮箱'},
                {field: 'sex', title: '性别',width: 80,align: 'center'},
                {field: 'head_pic', title: '头像',templet: function (d) {
                    return '<a href="javascript:void(0);"><image style="max-width:26px;max-height:26px;" src="'+d.head_pic+'"/></a>';
                }, width: 80,align: 'center'},
                {field: 'role_id', templet: function (d) {
                    var str = '<span class="layui-badge-rim">' + d.rolename + '</span>';
                    return str;
                }, title: '角色'},
                {field: 'add_time', sort: true, templet: function (d) {
                    return util.toDateString(d.add_time);
                }, title: '创建时间'},
                {field: 'status', templet: '#tableStateUser', title: '状态',width: 100,align: 'center'},
                {align: 'center', toolbar: '#tableBarUser', title: '操作', minWidth: 250}
            ]]
        });

        //开启关闭
        form.on('switch(ckStateUser)', function(obj){
            var dataid = $(this).attr('data-id');
            var pcfurl = '{:url("/ajax/ajax_changeTableVal")}';
            if(dataid == 1 || {$admin_id} == dataid){
              var openstatus = 1;
            }
            if (obj.elem.checked){
                $(this).val(1);
            }else{
                $(this).val(0);
            }
            changeTableVal('admin','admin_id',dataid,'status',this,pcfurl,'admin/index',openstatus);
        });

        // 搜索
        form.on('submit(formSubSearchUser)', function (data) {
            insTb.reload({where:data.field,page:{curr: 1}},'data');
        });

        $(document).on("click","#LAY-component-layer-list .layui-btn",function(){
          var type = $(this).data('type');
          active[type] && active[type].call(this);
        });

        // 触发事件
        var active = {
            admin_add: function(){
                layer.open({
                    type: 2,
                    title: '添加管理员',
                    fixed: true,
                    shadeClose: false,
                    shade: 0.3,
                    area: ['100%', '100%'],
                    content: '{:url("/admin/add")}'
                });
            },
            admin_edit: function(){
                var id = $(this).attr('data-id');
                layer.open({
                    type: 2,
                    title: '修改管理员',
                    fixed: true,
                    shadeClose: false,
                    shade: 0.3,
                    area: ['100%', '100%'],
                    content: '{:url("/admin/edit")}?id='+id
                });
            },
            admin_del: function(){
                delfun(this);
            },
            admin_reset: function(){
                var id = $(this).attr('data-id');
                var nickName = $(this).attr('data-name');
                layer.confirm('确定要重置“' + nickName + '”的登录密码吗？', {
                    skin: 'layui-layer-admin',
                    shade: .1
                }, function (i) {
                    layer.close(i);
                    layer.msg('正在处理请勿刷新页面', {icon: 16,shade: 0.01,time:3600000});
                    $.get("{:url('/admin/restPsw')}",{admin_id:id}, function (res) {
                        layer.closeAll('loading');
                        if (res.status) {
                            layer.msg(res.msg, {icon: 1,time:1000});
                        } else {
                            layer.msg(res.msg, {icon: 2,time:1000});
                        }
                    }, 'json');
                });
            }
        };
        
    });
</script>
</body>
</html>